package dao;

import model.FileInformation;
import util.Config;

import java.sql.SQLException;
import java.util.*;

/**
 * PackageName: dao
 * ClassName: FileInformationDao
 * Author: tum0r
 * Time: 2020/5/24 16:22
 */
public class FileInformationDao extends BaseDao {

    public String check(FileInformation fileInformation) throws SQLException {
        String result = fileInformation.getUUID();
        LinkedList<LinkedList<Object>> temp = query("SELECT UUID FROM file_information WHERE ContentID = ?", fileInformation.getContentID());
        if (temp.size() == 1) {
            result = (String) temp.get(0).get(0);
        }
        return result;
    }

    public boolean add(FileInformation fileInformation) throws SQLException {
        boolean result = false;
        String UUID = check(fileInformation);
        if (UUID.equals(fileInformation.getUUID())) {
            result = update("INSERT INTO file_information VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                    fileInformation.getUUID(),
                    fileInformation.getName(),
                    fileInformation.getType(),
                    fileInformation.getCreateTime(),
                    fileInformation.getLastAccessTime(),
                    fileInformation.getLabel(),
                    fileInformation.getSize(),
                    fileInformation.getUnit(),
                    fileInformation.getContentID(),
                    fileInformation.getFingerprint()) == 1;
        }
        return result;
    }

    public LinkedList<FileInformation> search(String[] keys) throws SQLException {
        StringBuilder command = new StringBuilder();
        command.append("SELECT UUID, Name, Type, Label, Size, Unit, CreateTime FROM file_information WHERE 1=1 ");
        if (keys != null) {
            for (int i = 0; i < keys.length; i++) {
                if (keys[i].startsWith("^")) {
                    command.append("AND Label NOT LIKE ?");
                    keys[i] = keys[i].substring(1);
                } else {
                    command.append("AND Label LIKE ?");
                }
                keys[i] = "%" + keys[i] + "%";
            }
        }
        command.append("ORDER BY Name ASC, CreateTime DESC ");
        LinkedList<LinkedList<Object>> temp;
        if (keys != null) {
            temp = query(command.toString().trim(), keys);
        } else {
            temp = query(command.toString().trim());
        }
        LinkedList<FileInformation> result = new LinkedList<>();
        FileInformation item;
        for (LinkedList<Object> itemTemp : temp) {
            item = new FileInformation();
            item.setUUID((String) itemTemp.get(0));
            item.setName((String) itemTemp.get(1));
            item.setType((String) itemTemp.get(2));
            item.setLabel((String) itemTemp.get(3));
            item.setSize((Double) itemTemp.get(4));
            item.setUnit((String) itemTemp.get(5));
            item.setCreateTime(new Date((long) itemTemp.get(6)));
            result.add(item);
        }
        return result;
    }

    private FileInformation[] getFileInformation(LinkedList<LinkedList<Object>> temp) throws SQLException {
        FileInformation[] result = null;
        if (temp.size() != 0) {
            result = new FileInformation[temp.size()];
            FileInformation item;
            for (int i = 0; i < result.length; i++) {
                LinkedList<Object> tempItem = temp.get(i);
                item = new FileInformation();
                item.setUUID((String) tempItem.get(0));
                item.setName((String) tempItem.get(1));
                item.setType((String) tempItem.get(2));
                if (tempItem.get(3) != null) {
                    item.setCreateTime(new Date((long) tempItem.get(3)));
                }
                if (tempItem.get(4) != null) {
                    item.setLastAccessTime(new Date((long) tempItem.get(4)));
                }
                item.setLabel((String) tempItem.get(5));
                item.setSize((Double) tempItem.get(6));
                item.setUnit((String) tempItem.get(7));
                item.setContentID((String) tempItem.get(8));
                item.setFingerprint((String) tempItem.get(9));
                result[i] = item;
                update("UPDATE file_information SET LastAccessTime = ? WHERE UUID = ?", new Date(), item.getUUID());
                commit();
            }
        }
        return result;
    }

    public LinkedList<FileInformation> getFileInformationByID(String[] UUID) throws SQLException {
        LinkedList<FileInformation> result = new LinkedList<>();
        FileInformation[] item;

        if (UUID != null && UUID.length != 0) {
            for (int i = 0; i < UUID.length; i++) {
                LinkedList<LinkedList<Object>> temp = query("SELECT * FROM file_information WHERE UUID = ?", UUID[i]);
                item = getFileInformation(temp);
                if (item != null) {
                    result.addAll(Arrays.asList(item));
                }
            }
        } else {
            LinkedList<LinkedList<Object>> temp = query("SELECT * FROM file_information");
            item = getFileInformation(temp);
            if (item != null) {
                result.addAll(Arrays.asList(item));
            }
        }
        return result;
    }

    public int delete(String[] UUID) throws SQLException {
        int result = 0;
        if (UUID != null && UUID.length != 0) {
            for (String s : UUID) {
                if (s == null) continue;
                LinkedList<LinkedList<Object>> temp = query("SELECT ContentID FROM file_information WHERE UUID = ?", s);
                if (temp.size() == 1) {
                    String[] contentID = temp.get(0).get(0).toString().split(" ");
                    for (int i = 0; i < contentID.length; i++) {
                        temp = query("SELECT 1 FROM file_information WHERE ContentID LIKE ?", "%" + contentID[i] + "%");
                        if (temp.size() <= 1) {
                            update("DELETE FROM file_content WHERE UUID = ?", contentID[i]);
                        }
                    }
                }
                result += update("DELETE FROM file_information WHERE UUID = ?", s);
            }
        } else {
            update("DELETE FROM file_content");
            result = update("DELETE FROM file_information");
        }
        return result;
    }

}
